﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Model.Entity;//实体
using Gx.Common;//通用类
using Gx.Model;
using Gx.Dal.Employee;
using Gx.Model.Enums;
using System.Linq;
using Gx.Bll.Shared;

namespace Gx.Bll.Employee
{
    public class G_StockOutBackInfoBll
    {
        public ReturnModel<bool> AddG_StockOutBackInfo(string data)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (string.IsNullOrEmpty(data))
                {
                    return r;
                }
                G_StockOutBackInfo model = JsonHelper.DeserializeObject<G_StockOutBackInfo>(data);
                if (model == null)
                {
                    r.Code = (int)Code.参数错误;
                    r.Msg = "参数错误";
                    return r;
                }
                if (model.StockOutId < 1)
                {
                    r.Msg = "请选择有效的出库单";
                    return r;
                }
                if (model.GoodsList == null && model.GoodsList.Count == 0)
                {
                    r.Msg = "请选择物料";
                    return r;
                }
                if (!model.GoodsList.Exists(x => x.GoodsNum > 0))
                {
                    r.Msg = "入库物料必须有一条大于0";
                    return r;
                }
                if (model.CheckUser < 0)
                {
                    r.Msg = "请选择复核人";
                    return r;
                }
                if (model.GoodsList.GroupBy(x => x.GoodsId).Any(x => x.Count() > 1))
                {
                    r.Msg = "物料不能重复";
                    return r;
                }
                G_StockOutInfo go = new G_StockOutInfoBll().GetG_StockOutInfoModel(model.StockOutId);
                if (go.Status > 0 || go.BillStatus == (int)BillStatus.作废)
                {
                    r.Msg = "采购单不存在,请重新选择";
                    return r;
                }
                if (go.CheckStatus != (int)CheckStatus.审批通过)
                {
                    r.Msg = "采购单未审批完成，请审批完成在入库";
                    return r;
                }
                List<int> goodsIdList = model.GoodsList.Select(x => x.GoodsId).ToList();
                List<G_GoodsInfo> goodsList = new G_GoodsInfoBll().GetG_GoodsInfoList(goodsIdList);
                model.AddTime = model.LastEditTime = DateTime.Now;
                model.EmployeeId = model.AddUser = model.LastEditUser = LoginHelper.GetUserId();
                model.CheckStatus = (int)CheckStatus.审批中;
                model.BillStatus = (int)BillStatus.正常;
                model.BillNo = DateTime.Now.ToStringyyyyMMddHHmmfff("FC");
                model.GoodsId = goodsIdList.Join();
                model.GoodsName = string.Join(",", goodsList.Select(x => x.GoodsName));
                int StockOutBackId = dal.AddG_StockOutBackInfo(model);
                if (StockOutBackId == 0)
                {
                    r.Code = (int)Code.服务端错误;
                    r.Msg = "服务端错误";
                    return r;
                }
                new M_LogInfoBll().AddOptionsLog("AddG_StockOutBackInfo", "添加一条返仓单信息", data);
                G_StockOutBackGoodsInfoBll goodsBll = new G_StockOutBackGoodsInfoBll();
                foreach (var item in model.GoodsList)
                {
                    item.StockOutBackId = StockOutBackId;
                    item.GoodsName = goodsList.Find(x => x.GoodsId == item.GoodsId).GoodsName;
                    goodsBll.AddG_StockOutBackGoodsInfo(item);
                }
                r.Code = (int)Code.操作成功;
                r.Msg = "操作成功";
                r.Data = true;
                r.Success = true;
                return r;
            }
        }


        public ReturnModel<bool> DelG_StockOutBackInfo_BillStatus(int stockOutBackId)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                G_StockOutBackInfo model = new G_StockOutBackInfo
                {
                    LastEditTime = DateTime.Now,
                    LastEditUser = LoginHelper.GetUserId(),
                    BillStatus = (int)BillStatus.作废,
                    StockOutBackId = stockOutBackId,
                };
                bool res = dal.DelG_StockOutBackInfo_BillStatus(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("DelG_StockOutBackInfo", "作废一条返仓单信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }


        private List<G_StockOutBackInfo> GetG_StockOutBackInfoList(string strWhere)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                return dal.GetG_StockOutBackInfoList(strWhere);
            }
        }





        public List<G_StockOutBackInfo> GetG_StockOutBackInfoList(string BillNo, int? CheckStatus, DateTime? StartCheckDate, DateTime? EndCheckDate,
            int? BillStatus,
        int pageIndex, int pageSize, out int totalCount)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                if (!BillStatus.IsNull() && BillStatus > -1)
                {
                    strWhere.AppendFormat("   AND BillStatus={0}", BillStatus);
                }
                if (!CheckStatus.IsNull() && CheckStatus > -1)
                {
                    strWhere.AppendFormat("   AND CheckStatus={0}", CheckStatus);
                }
                if (!BillNo.IsNullOrEmpty())
                {
                    strWhere.AppendFormat(" AND BillNo LIKE '%{0}%'", BillNo);
                }
                if (StartCheckDate.IsDate())
                {
                    strWhere.AppendFormat(" AND CheckDate>='{0}'", StartCheckDate);
                }
                if (EndCheckDate.IsDate())
                {
                    strWhere.AppendFormat(" AND CheckDate <= '{0}' ", EndCheckDate.Value.AddDays(1));
                }
                List<G_StockOutBackInfo> list = dal.GetG_StockOutBackInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
                foreach (var item in list)
                {
                    item.StrIsStockOutBack = item.IsStockOutBack ? "是" : "否";
                    item.StrBillStatus = EnumHelper.GetDataTypeDescription<BillStatus>(item.BillStatus);
                    item.StrAddTime = item.AddTime.ToStringyyyyMMdd();
                    item.StrCheckStatus = EnumHelper.GetDataTypeDescription<CheckStatus>(item.CheckStatus);
                }
                return list;
            }
        }



        private int GetG_StockOutBackInfoCount(string strWhere)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                return dal.GetG_StockOutBackInfoCount(strWhere);
            }
        }



        public G_StockOutBackInfo GetG_StockOutBackInfoModel(int stockOutBackId, bool isGetEmployee = false)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                G_StockOutBackInfo model = dal.GetG_StockOutBackInfoModel(stockOutBackId);
                if (isGetEmployee)
                {
                    List<int> empIdList = new List<int>() { model.CheckUser, model.EmployeeId };
                    List<E_EmployeeInfo> empList = new E_EmployeeInfoBll().GetE_EmployeeInfoList(empIdList);
                    model.EmpName = empList.Exists(x => x.EmployeeId == model.EmployeeId) ? empList.Find(x => x.EmployeeId == model.EmployeeId).EmpName : "员工不存在或已离职";
                    model.CheckUserName = empList.Exists(x => x.EmployeeId == model.CheckUser) ? empList.Find(x => x.EmployeeId == model.CheckUser).EmpName : "员工不存在或已离职";
                }
                return model;
            }
        }


        /// <summary>
        /// 审核单据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel<bool> EditG_StockOutBackInfo_CheckUser(G_StockOutBackInfo model)
        {
            using (G_StockOutBackInfoDal dal = new G_StockOutBackInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                model.CheckUser = model.LastEditUser = LoginHelper.GetUserId();
                model.CheckDate = model.LastEditTime = DateTime.Now;
                bool res = dal.EditG_StockOutBackInfo_CheckUser(model);
                if (!res)
                {
                    return r;
                }
                new M_LogInfoBll().AddOptionsLog("EditG_StockOutBackInfo_CheckUser", "审核一条返仓单", model);
                if (model.CheckStatus == (int)CheckStatus.审批通过)
                {
                    List<G_StockOutBackGoodsInfo> sigList = new G_StockOutBackGoodsInfoBll().GetG_StockOutBackGoodsInfoDetails(model.StockOutBackId);
                    G_StocksInfoBll siBll = new G_StocksInfoBll();
                    G_StocksTotalInfoBll stBll = new G_StocksTotalInfoBll();
                    foreach (var item in sigList)
                    {
                        siBll.AddG_StocksInfo(new G_StocksInfo()
                        {
                            Num = item.GoodsNum,
                            GoodsId = item.GoodsId,
                            ObjectId = item.StockOutBackId,
                            StockType = (int)StocksType.生产返仓单,
                        });
                        stBll.EditG_StocksTotalInfo(new G_StocksTotalInfo()
                        {
                            GoodsId = item.GoodsId,
                            Num = item.GoodsNum
                        });
                    }
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功,物料已入库!";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }

        }
    }
}
